package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.Server;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1PcQuest;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class logpcpower_new1 {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean BUILD_DATA = false;
  
  private static logpcpower_new1 _instance;
  
  public static final String TOKEN = ",";
  
  public static logpcpower_new1 getInstance() {
    if (_instance == null)
      _instance = new logpcpower_new1(); 
    return _instance;
  }
  
  public static void main(String[] a) {
    try {
      while (true)
        Server.main(null); 
    } catch (Exception exception) {
      return;
    } 
  }
  
  public static void forIntensifyArmor(L1PcInstance pc) {
    ArrayList<Object> aTempData = null;
    if (!BUILD_DATA) {
      BUILD_DATA = true;
      getData();
    } 
    L1PcQuest quest = pc.getQuest();
    int i = 0;
    while (i < aData.size()) {
      aTempData = aData.get(i);
      if (pc.getMeteLevel() == ((Integer)aTempData.get(0)).intValue()) {
        int[] giveItem = (int[])aTempData.get(1);
        int[] giveCount = (int[])aTempData.get(2);
        if (giveItem != null && giveCount != null) {
          int j = 0;
          while (j < giveItem.length) {
            L1ItemInstance item = ItemTable.get().createItem(giveItem[j]);
            if (item.isStackable()) {
              item.setCount(giveCount[j]);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCount[j]) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("获得转生奖励：" + item.getName() + " " + giveCount[j]));
            } 
            j++;
          } 
        } 
      } 
      i++;
    } 
  }
  
  private static void getData() {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstmt = conn.prepareStatement("SELECT * FROM william_pc_转生_giveitem");
      rs = pstmt.executeQuery();
      ArrayList<Object> aReturn = null;
      if (rs != null)
        while (rs.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rs.getInt("logpcpower")));
          aReturn.add(1, getArray(rs.getString("getItem"), ",", 1));
          aReturn.add(2, getArray(rs.getString("count"), ",", 1));
          aData.add(aReturn);
        }  
    } catch (SQLException sQLException) {
    
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstmt);
      SQLUtil.close(conn);
    } 
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
